package com.lxmes.base.service.impl;

import java.util.List;

import com.lxmes.common.exception.ServiceException;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.base.mapper.JcProductionLineMapper;
import com.lxmes.base.domain.JcProductionLine;
import com.lxmes.base.service.IJcProductionLineService;

import javax.validation.Validator;

/**
 * 生产线信息维护Service业务层处理
 *
 * @author ruoyi
 * @date 2022-11-29
 */
@Service
public class JcProductionLineServiceImpl implements IJcProductionLineService
{
    @Autowired
    private JcProductionLineMapper jcProductionLineMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询生产线信息维护
     *
     * @param id 生产线信息维护主键
     * @return 生产线信息维护
     */
    @Override
    public JcProductionLine selectJcProductionLineById(Long id)
    {
        return jcProductionLineMapper.selectJcProductionLineById(id);
    }

    /**
     * 查询生产线信息维护列表
     *
     * @param jcProductionLine 生产线信息维护
     * @return 生产线信息维护
     */
    @Override
    public List<JcProductionLine> selectJcProductionLineList(JcProductionLine jcProductionLine)
    {
        return jcProductionLineMapper.selectJcProductionLineList(jcProductionLine);
    }

    /**
     * 新增生产线信息维护
     *
     * @param jcProductionLine 生产线信息维护
     * @return 结果
     */
    @Override
    public int insertJcProductionLine(JcProductionLine jcProductionLine)
    {
        jcProductionLine.setCreateTime(DateUtils.getNowDate());
        return jcProductionLineMapper.insertJcProductionLine(jcProductionLine);
    }

    /**
     * 修改生产线信息维护
     *
     * @param jcProductionLine 生产线信息维护
     * @return 结果
     */
    @Override
    public int updateJcProductionLine(JcProductionLine jcProductionLine)
    {
        jcProductionLine.setUpdateTime(DateUtils.getNowDate());
        return jcProductionLineMapper.updateJcProductionLine(jcProductionLine);
    }

    /**
     * 批量删除生产线信息维护
     *
     * @param ids 需要删除的生产线信息维护主键
     * @return 结果
     */
    @Override
    public int deleteJcProductionLineByIds(Long[] ids)
    {
        return jcProductionLineMapper.deleteJcProductionLineByIds(ids);
    }

    /**
     * 删除生产线信息维护信息
     *
     * @param id 生产线信息维护主键
     * @return 结果
     */
    @Override
    public int deleteJcProductionLineById(Long id)
    {
        return jcProductionLineMapper.deleteJcProductionLineById(id);
    }

    @Override
    public String imporIteminfo(List<JcProductionLine> proLineList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(proLineList) || proLineList.size() == 0)
        {
            throw new ServiceException("导入物料数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (JcProductionLine line : proLineList)
        {
            try
            {
                BeanValidators.validateWithException(validator, line);
                line.setCreateBy(operName);
                line.setFactoryCode("BLD01");
                line.setFactoryName("博力达一号公司");
                line.setUpdateBy(operName);
                this.insertJcProductionLine(line);
                successNum++;
                successMsg.append("<br/>" + successNum + "、产线 " + line.getLineName() + " 导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + line.getLineName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
